﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using ZHDJ.Core.Log;
using ZHDJ.Data.Models.Common;
using ZHDJ.WinService.Job;

namespace ZHDJ.WinService.Util
{
    public static class ScheduleHelper
    {

        private static IScheduler _Scheduler;
        public static IScheduler Scheduler
        {
            get { return _Scheduler; }
            set { value = Scheduler; }
        }
        /// <summary>
        /// 运行的任务
        /// </summary>
        public static List<JobBase> JobBaseList;

        private static List<ScheduledJobInfo> _RunProcedureJobList;
        
        /// <summary>
        /// 定时作业 执行存储过程
        /// </summary>
        public static List<ScheduledJobInfo> RunProcedureJobList
        {
            get
            {
                

                    return _RunProcedureJobList;
               
            }
            set
            {
               
                    value = _RunProcedureJobList;
               
            }
        }
        static ScheduleHelper()
        {
            _Scheduler= StdSchedulerFactory.GetDefaultScheduler();
            JobBaseList = new List<JobBase>();
            _RunProcedureJobList = new List<ScheduledJobInfo>();
        }

        /// <summary>
        /// 增加任务
        /// </summary>
        public static void AddJob<T>(T job) where T :JobBase
        {
            try
            {
                IJobDetail jobDetail = JobBuilder.Create<T>()
                                       .WithIdentity(job.JobSetInfo.JobName, job.JobSetInfo.JobGroup)
                                       .Build();

                ITrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                    .WithIdentity(job.JobSetInfo.TriggerName, job.JobSetInfo.JobGroup)
                    .StartNow()
                    .WithCronSchedule(job.JobSetInfo.Cron)
                    .Build();

                Scheduler.ScheduleJob(jobDetail, trigger);
                JobBaseList.Add(job);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        }

        /// <summary>
        /// 运行任务
        /// </summary>
        /// <param name="name"></param>
        /// <param name="group"></param>
        public static void RunJob(string name, string group)
        {
            //var jobKey = new JobKey(name, group);
            //Scheduler.ResumeJob(jobKey);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="name"></param>
        /// <param name="group"></param>
        public static void DeleteJob(string name, string group)
        {
            var jobKey = new JobKey(name, group);
            Scheduler.PauseJob(jobKey);
            Scheduler.DeleteJob(jobKey);
        }

        public static void RemoveRunProcedureJobList(ScheduledJobInfo scheduledJobInfo)
        {
            RunProcedureJobList.Remove(scheduledJobInfo);
        }

        public static List<string> GetScheduledJobIDList()
        {
            var list = new List<string>();
            foreach (var item in RunProcedureJobList)
            {
                list.Add(item.JobFlag);
            }
            return list;
        }
    }
}
